package com.scpii.universal.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import com.scpii.universal.constant.GenericConstants;

/*
 * 数据流处理类
 * @yuejunyan
 */
public final class StreamUtil {

	private static final int BUFFER_SIZE = 2048;

	public final static byte[] convertToBytes(InputStream in) throws Exception {
		ByteArrayOutputStream reqOut = new ByteArrayOutputStream();
		byte[] buffer = new byte[BUFFER_SIZE];
		int count = 0;
		while ((count = in.read(buffer, 0, BUFFER_SIZE)) != -1) {
			reqOut.write(buffer, 0, count);
		}
		byte[] req = reqOut.toByteArray();
		reqOut.close();
		return req;
	}

	public final static OutputStream writeBytesToStream(OutputStream out,
			byte[] in) throws Exception {
		int offer = 0;
		while (offer + BUFFER_SIZE <= in.length) {
			out.write(in, offer, BUFFER_SIZE);
			offer += BUFFER_SIZE;
		}
		int length = in.length - offer;
		out.write(in, offer, length);
		out.flush();
		out.close();
		return out;
	}
	
	/***
	 * 压缩GZip
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] gZip(byte[] data) {
		byte[] b = null;
		try {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			GZIPOutputStream gzip = new GZIPOutputStream(bos);
			gzip.write(data);
			gzip.finish();
			gzip.close();
			b = bos.toByteArray();
			bos.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return b;
	}

	/***
	 * 解压GZip
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] unGZip(byte[] data) {
		byte[] b = null;
		try {
			ByteArrayInputStream bis = new ByteArrayInputStream(data);
			GZIPInputStream gzip = new GZIPInputStream(bis);
			byte[] buf = new byte[BUFFER_SIZE];
			int num = -1;
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			while ((num = gzip.read(buf, 0, buf.length)) != -1) {
				baos.write(buf, 0, num);
			}
			b = baos.toByteArray();
			baos.flush();
			baos.close();
			gzip.close();
			bis.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return b;
	}

	/***
	 * 压缩Zip
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] zip(byte[] data) {
		byte[] b = null;
		try {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			ZipOutputStream zip = new ZipOutputStream(bos);
			ZipEntry entry = new ZipEntry("zip");
			entry.setSize(data.length);
			zip.putNextEntry(entry);
			zip.write(data);
			zip.closeEntry();
			zip.close();
			b = bos.toByteArray();
			bos.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return b;
	}

	/***
	 * 解压Zip
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] unZip(byte[] data) {
		byte[] b = null;
		try {
			ByteArrayInputStream bis = new ByteArrayInputStream(data);
			ZipInputStream zip = new ZipInputStream(bis);
			while (zip.getNextEntry() != null) {
				byte[] buf = new byte[1024];
				int num = -1;
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				while ((num = zip.read(buf, 0, buf.length)) != -1) {
					baos.write(buf, 0, num);
				}
				b = baos.toByteArray();
				baos.flush();
				baos.close();
			}
			zip.close();
			bis.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return b;
	}
	/**
	 * 方法用于文件上传或者是将文件转存
	 * @param file
	 * @param path
	 * @throws Exception
	 */
	public static void uploadFileToServer(File file,String path)throws Exception{
		if (file!=null && (path!=null && path.length()>0)) {
			FileInputStream fileInputStream = new FileInputStream(file);
			int length = fileInputStream.available();
			byte[] dataBytes = new byte[length];
			fileInputStream.read(dataBytes);
			fileInputStream.close();
			//输出到文件中
			File newFile = new File(path);
			FileOutputStream fileOutputStream = new FileOutputStream(newFile);
			fileOutputStream.write(dataBytes);
			fileOutputStream.flush();
			fileOutputStream.close();
		} else {
			throw (new Exception("File is empty Or Path is null"));
		}
	}
	
	/**
	 * 上传图片
	 * 
	 * @param activeImgFIle
	 * @param toPath
	 * @return
	 */
	public static String uploadActiveImgs(File activeImgFIle, String toPath) {
		String serverPath = "";
		try {
			String serverUrl = EhCacheManager.getObject(GenericConstants.SERVER_URL_PREFIX).toString();
			if (activeImgFIle != null && toPath != null) {
				uploadFileToServer(activeImgFIle, toPath);
				serverPath = serverUrl	+ toPath;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return serverPath;
	}
}
